package com.kun.video.ui;

import cn.hutool.core.util.StrUtil;
import com.kun.video.util.ComUtil;
import com.kun.video.util.CssManager;
import javafx.beans.value.ChangeListener;
import javafx.scene.Parent;
import javafx.scene.layout.Region;

/**
 * 自定义组件接口
 * <p>
 * 示例子类结构 -> {@link SampleView}
 *
 * @author gzc
 * @since 2025/2/9
 **/
public interface KunComponent {

    default void initialize() {
        ComUtil.uiThreadRun(() -> {
            initStyleClass();
            initAnimation();
            initLayout();
            initStyle();
            initDimensions();
            initListeners();
            initData();
            if (getNode() instanceof Region region) {
                dimensionsListener(region);
            }
        });
    }

    Parent getNode();

    String getStyleName();

    default void initStyleClass() {
        String styleName = getStyleName();
        Parent parent = getNode();
        if (parent != null && StrUtil.isNotBlank(styleName)) {
//            String simpleName = parent.getClass().getSimpleName();
//            System.out.println(simpleName);
            parent.getStyleClass().add(styleName);
            parent.getStylesheets().add(CssManager.CSS_FILE_PARENT_PATH + styleName + ".css");
        }
    }

    default void initAnimation() {
    }

    default void initStyle() {
    }

    void initLayout();

    void initDimensions();

    void reactiveDimensions(double width, double height);

    void initData();

    default void dimensionsListener(Region region) {
        if (region == null) {
            return;
        }
        ChangeListener<Number> widthChangeListener = (obs, old, now) -> reactiveDimensions(now.doubleValue(), region.getHeight());
        ChangeListener<Number> heightChangeListener = (obs, old, now) -> reactiveDimensions(region.getWidth(), now.doubleValue());
        region.widthProperty().addListener(widthChangeListener);
        region.heightProperty().addListener(heightChangeListener);
    }

    void initListeners();

    /**
     * 释放资源
     */
    void dispose();


}
